﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RealEstate.Models;
using RealEstate.Common;

namespace RealEstate
{
    public partial class LoanDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            if (Request.Params["id"] != null && int.TryParse(Request.Params["id"].ToString(), out id))
            {
                if (!Roles.IsUserInRole("admin"))
                {
                    pnlMyIncomeInfo.Visible = false;
                    imgEdit.Visible = false;
                    var user = Membership.GetUser();
                    if (user != null)
                    {
                        var clientEntity = new ClientEntities();
                        var client = clientEntity.GetByUserId(Guid.Parse(user.ProviderUserKey.ToString()));
                        var entity = new TransactionEntities();
                        var item = entity.GetById(id);

                        if (!(item.ClientId == client[0].ClientId || item.CoClientId == client[0].ClientId))
                            Response.Redirect("~/Views/404.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/Views/Account/Login.aspx");
                    }
                }

                LoadData(id);
                hplHistory.Attributes.Add("onclick", string.Concat("openWin('/views/transaction/StatusHistory.aspx?id=", id, "'); return false;"));
            }

        }

        private void LoadData(int id)
        {
            TransactionEntities entity = new TransactionEntities();
            TRANSACTION item = entity.GetById(id);
            if (item != null)
            {
                //Case Information
                if (item.DateOpen != null)
                    lblDateOpen.Text = Utilities.ConvertDateTime(item.DateOpen);
                if (item.CaseIDNumber != null)
                    lblCaseIDNumber.Text = item.CaseIDNumber.ToString();

                //Loan Broker Info
                lblLoanBrokerName.Text = item.LoanBrokerName;
                lblLoanCompany.Text = item.LoanCompany;

                //ClientInformation
                if (item.Client != null)
                {
                    lblClientLastName.Text = item.Client.LastName;
                    lblClientFirstName.Text = item.Client.FirstName;
                    lblClientSSN.Text = item.Client.SSN;
                    lblClientWorkPhone.Text = item.Client.WorkPhNumber;
                    lblClientFaxNumber.Text = item.Client.FaxNumber;
                    lblClientHomePhone.Text = item.Client.HomePhNumber;
                    lblClientCellPhone.Text = item.Client.CellNumber;
                    lblClientEmail.Text = item.Client.Email;
                    if (item.Client.BirthDate != null)
                        lblClientBirthDate.Text = Utilities.ConvertDateTime(item.Client.BirthDate);
                }

                if (item.Client1 != null)
                {
                    lblCoClientLastName.Text = item.Client1.LastName;
                    lblCoClientFirstName.Text = item.Client1.FirstName;
                    lblCoClientSSN.Text = item.Client1.SSN;
                    lblCoClientWorkPhone.Text = item.Client1.WorkPhNumber;
                    lblCoClientFaxNumber.Text = item.Client1.FaxNumber;
                    lblCoClientHomePhone.Text = item.Client1.HomePhNumber;
                    lblCoClientCellPhone.Text = item.Client1.CellNumber;
                    lblCoClientEmail.Text = item.Client1.Email;
                    if (item.Client1.BirthDate != null)
                        lblCoClientBirthDate.Text = Utilities.ConvertDateTime(item.Client1.BirthDate);
                }

                //Property Information                 
                if (item.Property1 != null)
                {
                    lblSubjectPropertyType.Text = item.Property1.PropertyType.PropertyType1;
                    lblSubjectStreetNumber.Text = item.Property1.StreetNumer;
                    lblCity.Text = item.Property1.City.City1;
                    lblSubjectStreetName.Text = item.Property1.StreetName;
                    lblZipCode.Text = item.Property1.ZipCode;
                }

                if (item.Property2 != null)
                {
                    lblMailStreetNumber.Text = item.Property2.StreetNumer;
                    lblMailStreetName.Text = item.Property2.StreetName;
                    lblCity_New.Text = item.Property2.City.City1;
                    lblZipCode_New.Text = item.Property2.ZipCode;
                }

                //Transaction Details
                if (item.PurchaseSoldPrice != null)
                    lblPurchaseSoldPrice.Text = Utilities.ConvertCurrency(item.PurchaseSoldPrice);

                if (item.TotalEquity != null)
                    lblTotalEquity.Text = Utilities.ConvertCurrency(item.TotalEquity);

                lblProgram.Text = item.Program;
                lblPreviousProgram.Text = item.PreviousProgram;

                if (item.DateOfCOE != null)
                    lblDateOfCOE.Text = Utilities.ConvertDateTime(item.DateOfCOE);

                lblLoanAmount.Text = item.LoanAmount.ToString();

                if (item.DateLocked != null)
                    lblDateOfCOE.Text = Utilities.ConvertDateTime(item.DateLocked);

                lblPreviousCOE.Text = item.PreviousCOE;
                lblTargetProgram.Text = item.TargetProgram;

                //Financial Info
                if (item.HouseHoldGrossIncome != null)
                    lblHouseHoldIncome.Text = Utilities.ConvertCurrency(item.HouseHoldGrossIncome);

                if (item.HouseHoldDebits != null)
                    lblHouseHoldDebits.Text = Utilities.ConvertCurrency(item.HouseHoldDebits);

                //TransactionStatus_RE
                if (item.Status != null)
                    lblCaseStatus.Text = item.Status.Status1;
                if (item.DateOfStatus != null)
                    lblDateOfStatus.Text = Utilities.ConvertDateTime(item.DateOfStatus);

                //My Income Info
                if (item.TotalGrossIncome != null)
                    lblTotalGrossIncome.Text = Utilities.ConvertCurrency(item.TotalGrossIncome);
                if (item.CreditToClient != null)
                    lblCreditToClient.Text = Utilities.ConvertCurrency(item.CreditToClient);

                lblWayToCredit.Text = item.WayToCredit;

                if (item.Expense != null)
                    lblExpense.Text = Utilities.ConvertCurrency(item.Expense);

                lblExpenseType.Text = item.ExpenseType;
                if (item.AssistantFee != null)
                    lblPayToAssistant.Text = Utilities.ConvertCurrency(item.AssistantFee);
                lblAssistantName.Text = item.AssisistantName;
                if (item.ReferalFee != null)
                    lblReferalFee.Text = Utilities.ConvertCurrency(item.ReferalFee);
                if (item.GrossCommission != null)
                    lblGrossCommission.Text = Utilities.ConvertCurrency(item.GrossCommission);
                if (item.MyGrossIncome != null)
                    lblMyGrossIncome.Text = Utilities.ConvertCurrency(item.MyGrossIncome);
                if (item.OtherIncome != null)
                    lblOtherIncome.Text = Utilities.ConvertCurrency(item.OtherIncome);
                if (item.TotalGrossIncome != null)
                    lblTotalGrossIncome.Text = Utilities.ConvertCurrency(item.TotalGrossIncome);
                if (item.TransactionFee != null)
                    lblTransactionFee.Text = Utilities.ConvertCurrency(item.TransactionFee);
                if (item.TransactionFee != null)
                    lblMyTotalGrossIncome.Text = item.MyTotalGrossIncome;

                lblReasonForOtherIncome.Text = item.ReasonForOtherIncome;
                //Notes
                lblNotes.Text = item.Notes;
            }
        }

        protected void imgEdit_Click(object sender, ImageClickEventArgs e)
        {
            int id = 0;
            int viewId = 0;
            if (Request.Params["id"] != null && int.TryParse(Request.Params["id"].ToString(), out id))
            {
                if (Request.Params["viewid"] != null && int.TryParse(Request.Params["viewid"].ToString(), out viewId))
                {
                    Response.Redirect(string.Concat("EditLoan.aspx?id=", id, "&viewid=", viewId));
                }
                else
                {
                    Response.Redirect(string.Concat("EditLoan.aspx?id=", id, "&viewid=", Constants.ViewByLoanStatusId));
                }
            }
            else
            {
                Response.Redirect("ByLoanStatus.aspx");
            }
        }

        protected void imgBack_Click(object sender, ImageClickEventArgs e)
        {
            int id = 0;
            if (Request.Params["viewid"] != null && int.TryParse(Request.Params["viewid"].ToString(), out id))
            {
                Response.Redirect(Utilities.GetViewName(id));
            }
            else
            {
                Response.Redirect("/default.aspx");
            }
        }
    }
}
